<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div class="search">
                <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                    <el-form-item label="申请日期：" prop="timeDate">
                        <el-date-picker v-model="queryParams.timeDate" type="daterange" range-separator="至"
                            start-placeholder="开始时间" end-placeholder="结束时间" @change="handleTimeChange"
                            format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
                    </el-form-item>
                    <el-form-item label="摘要来源：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="审核状态：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="对账状态：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="申请原因：" prop="workName">
                        <el-input v-model="queryParams.workName" placeholder="请输入"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <template #header>
                <el-row :gutter="20">
                    <el-col :span="1.5">
                        <el-button @click="addApplyVisible = true" type="success">新建申请</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button @click="payrollDistributionVisible = true" type="warning">发放工资</el-button>
                    </el-col>
                </el-row>
            </template>
            <el-table v-loading="loading" :data="dataList" stripe>
                <el-table-column label="申请日期" prop="createTime" align="center"></el-table-column>
                <el-table-column label="申请人" prop="createByName" align="center"></el-table-column>
                <el-table-column label="申请金额" prop="applyAmount" align="center"></el-table-column>
                <el-table-column label="摘要" prop="summary" align="center">
                    <template #default="scope">
                        {{ miscellaneous_summary.filter((item: any) => item.value == scope.row.summary)[0].label }}
                    </template>
                </el-table-column>
                <el-table-column label="收款方名称" prop="collectName" align="center"></el-table-column>
                <el-table-column label="申请原因" prop="applyReason" align="center"></el-table-column>
                <el-table-column label="查看凭证" align="center">
                    <template #default="scope">
                        <el-button @click="viewCredentials(scope.row)" type="primary" link>
                            查看凭证
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="审核状态" prop="customizedName" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.approvalStatus == 0" style="color: #E6A33F;">未审核</span>
                        <span v-else-if="scope.row.approvalStatus == 1" style="color: #68C23D;">同意</span>
                        <span v-else-if="scope.row.approvalStatus == 2" style="color: #F75F51;">拒绝</span>
                    </template>
                </el-table-column>
                <el-table-column label="对账状态" prop="customizedName" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.reconciliationFlag == 0" style="color: #F75F51;">未对账</span>
                        <span v-else-if="scope.row.reconciliationFlag == 1" style="color: #DF9BF9;">已对账</span>
                        <span v-else-if="scope.row.reconciliationFlag == 2" style="color: #68C23D;">已回款</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="120px">
                    <template #default="scope">
                        <el-tooltip content="审批">
                            <el-button @click="handleApproval(scope.row)" link type="warning">
                                <i style="font-size: 18px;" class="iconfont icon-daifukuan"></i>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip content="对账">
                            <el-button @click="handleReconciliation(scope.row)" link type="danger">
                                <i style="font-size: 18px;" class="iconfont icon-chehui-1"></i>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button @click="handleDelete(scope.row)" :style="{ fontSize: '18px' }" link type="danger"
                                icon="CircleClose"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <Voucher :visible="voucherVisible" :initList="voucherObj" @close="voucherVisible = false" />
        <AddApply :visible="addApplyVisible" :subsidiariesList="subsidiariesList" :initList="miscellaneous_summary"
            @submit="getList" @close="addApplyVisible = false" />
        <payrollDistribution :visible="payrollDistributionVisible" @close="payrollDistributionVisible = false" />
        <Reconciliation :visible="reconciliationVisible" @submit="getList" :initList="reconciliationObj"
            :subsidiariesList="subsidiariesList" @close="reconciliationVisible = false" />
        <Approval :visible="approvalVisible" :subsidiariesList="subsidiariesList" :initList="approvalObj"
            :summaryList="miscellaneous_summary" @submit="getList" @close="approvalVisible = false" />
    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';

import { getMiscellaneousExpensesListApi, getMiscellaneousExpensesPictureApi, deleteMiscellaneousExpensesApi } from '@/api/sj/financialManagement/miscellaneousExpenses'
import { getAllSubsidiaryApi } from '@/api/sj/warehouse/auxiliary';


// 查看凭证
import Voucher from './components/voucher.vue';

/** 新建申请 */
import AddApply from './components/addApply.vue';

/** 发放工资 */
import payrollDistribution from './components/payrollDistribution.vue';

/** 对账 */
import Reconciliation from './components/reconciliation.vue';
import Approval from './components/approval.vue';
import { init } from 'echarts';


const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const queryFormRef = ref<FormInstance>();
const { miscellaneous_summary } = toRefs<any>(proxy?.useDict('miscellaneous_summary'));

const data = reactive<pageDataOnlyQuery<any>>({
    queryParams: {
        timeDate: '',
        startDate: '',
        endDate: '',
        pageSize: 10,
        pageNum: 1
    }
});

const { queryParams } = toRefs(data);

const loading = ref(false);
const dataList = ref<any>([]);
const totalData = ref(0);
const voucherVisible = ref(false);
const addApplyVisible = ref(false);
const payrollDistributionVisible = ref(false);
const reconciliationVisible = ref(false);
const approvalVisible = ref(false);
const voucherObj = ref<any>();
const approvalObj = ref<any>();
const subsidiariesList = ref<any>([]);
const reconciliationObj = ref<any>({});

const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    queryParams.value.timeDate = '';
    handleQuery();
};

/** 列表删除 */
const handleDelete = (row: any) => {
    ElMessageBox.confirm('删除后此付款申请会完全取消，您确认要这么做么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            try {
                const { code } = await deleteMiscellaneousExpensesApi(row.id);
                if (code == 200) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功!'
                    });
                    getList();

                }
            } catch (error) {

            }

        })
        .catch(() => { });
};

/** 审批 */
const handleApproval = (row: any) => {
    approvalObj.value = row;
    approvalVisible.value = true;
};

/** 查看凭证 */
const viewCredentials = async (row: any) => {

    try {
        const { data } = await getMiscellaneousExpensesPictureApi(row.id);
        voucherObj.value = data;
        voucherVisible.value = true;

    } catch (error) {

    }
};

/** 对账 */
const handleReconciliation = (row: any) => {
    reconciliationObj.value = row;
    reconciliationVisible.value = true;
};


/** 日期选择 */
const handleTimeChange = (val: any) => {
    queryParams.value.startDate = val[0];
    queryParams.value.endDate = val[1];
};

const getList = async () => {
    loading.value = true;
    try {
        const { rows, total } = await getMiscellaneousExpensesListApi(queryParams.value);
        dataList.value = rows;
        totalData.value = total;
        loading.value = false;
    } catch (error) {
        loading.value = false;
    }
};

/** 获取所有子公司 */
const getCompanyList = async () => {
    const { data } = await getAllSubsidiaryApi();
    subsidiariesList.value = data;
};

onMounted(() => {
    getList();
    getCompanyList();

});

</script>
<style lang="scss" scoped></style>